$(function () {
    $.getJSON("/recentMember", function (result) {
        console.log(result);

        var ctx, data, myLineChart1, myLineChart2, options;
        $("#recentMemberCount").text("近七天新增了" + result["memberCount"] + "名会员");
        $("#CostCountByRecent").text("近七天消费了" + result["CostCount"].toFixed(2) + "元");
        $("#CostCount").text("所有会员合计消费了" + result["allMemberCount"] + "元");

        Chart.defaults.global.responsive = true;
        ctx = $('#jumbotron-line-chart1').get(0).getContext('2d');
        ctx2 = $('#jumbotron-line-chart2').get(0).getContext('2d');

        options = {
            showScale: false,
            scaleShowGridLines: false,
            scaleGridLineColor: "rgba(0,0,0,.05)",
            scaleGridLineWidth: 0,
            scaleShowHorizontalLines: false,
            scaleShowVerticalLines: false,
            bezierCurve: false,
            bezierCurveTension: 0.4,
            pointDot: false,
            pointDotRadius: 0,
            pointDotStrokeWidth: 2,
            pointHitDetectionRadius: 20,
            datasetStroke: true,
            datasetStrokeWidth: 4,
            datasetFill: true,
            legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<datasets.length; i++){%><li><span style=\"background-color:<%=datasets[i].strokeColor%>\"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>"
        };

        data = {
            labels: result["week"],
            datasets: [
                {
                    label: "My Second dataset",
                    fillColor: "rgba(34, 167, 240,0.2)",
                    strokeColor: "#22A7F0",
                    pointColor: "#22A7F0",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "#22A7F0",
                    data: result["memberJoin"]
                }
            ]
        };
        data2 = {
            labels: result["week"],
            datasets: [
                {
                    label: "My Second dataset",
                    fillColor: "rgba(34, 167, 240,0.2)",
                    strokeColor: "#22A7F0",
                    pointColor: "#22A7F0",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "#22A7F0",
                    data: result["sevenday"]
                }
            ]
        };
        myLineChart1 = new Chart(ctx).Line(data, options);
        myLineChart2 = new Chart(ctx2).Line(data2, options);
    })
});


var currentGid;

function modifyClient(obj) {
    var radio = $(".gstate");
    $("#create").fadeOut();
    $("#warning").slideDown(500);
    $.getJSON("getclient", {
        gid: obj.value
    }, function (result) {
        console.log(result);
        currentGid = obj.value;
        $("#gname").val(result["obj"]["Name"]);
        $("#gprice").val(result["obj"]["Phone"]);
        if (result["obj"]["Level"] === 1) {
            radio[0].click()
        } else if (result["obj"]["Level"] === 2) {
            radio[1].click()
        } else if (result["obj"]["Level"] === 3) {
            radio[2].click()
        }
    })
}

//修改
$(function () {
    var modal = $("#warning");
    $("#miniclose").click(function () {
        modal.fadeOut()
    });
    $("#gdelete").click(function () {
        $.getJSON("deleteclient", {
            gid: currentGid
        }, function (result) {
            console.log(result);
            console.log(result["err"]);
            if (result["err"] === null) {
                alert("删除成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    });
    $("#gcancle").click(function () {
        modal.fadeOut(500);
    });
    $("#gsure").click(function () {
        var radio = $(".gstate");
        var state;
        if (radio[0].checked === true) {
            state = 1
        } else if (radio[1].checked === true) {
            state = 2
        } else if (radio[2].checked === true) {
            state = 3
        }
        var data = {
            id: currentGid,
            name: $("#gname")[0].value,
            phone: $("#gprice")[0].value,
            level: state,
            motion: "update",
        };
        $.getJSON("updateclient", data, function (result) {
            console.log(result);
            if (result["err"] === null && result["row"] === 1) {
                alert("修改成功!");
                window.location.reload()
            } else {
                modal.fadeOut(500);
            }
        })
    })
});

$(function () {
    modal = $("#create");
    $("#creategoods").click(function () {
        $("#warning").fadeOut(500);
        modal.slideDown()
    });

    $("#gcancle2").click(function () {
        modal.fadeOut(500);
    });
    $("#gsure2").click(function () {
        var radio = $(".gstate2");
        var state;
        if (radio[0].checked === true) {
            state = 1
        } else if (radio[1].checked === true) {
            state = 2
        } else if (radio[2].checked === true) {
            state = 3
        }
        var data = {
            name: $("#gname2")[0].value,
            phone: $("#gprice2")[0].value,
            level: state,
            motion: "create",
        };
        $.getJSON("updateclient", data, function (result) {
            console.log(result);
            if (result["err"] === null && result["row"] === 1) {
                alert("增加成功!");
                window.location.reload()
            } else {
                alert("增加失败!");
                modal.fadeOut(500);
            }
        })
    });
    $("#miniclose2").click(function () {
        modal.fadeOut()
    })
});
